//
//  ZGLSearchTableViewCell.m
//  Faker
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZGLSearchTableViewCell.h"
#import "ZGLOptionsModel.h"
#import "ZGLSearchWebModel.h"

@interface ZGLSearchTableViewCell ()


@property (nonatomic, strong) NSMutableArray *signArr;
@property (nonatomic, strong) NSDictionary *dict;
@property (nonatomic, strong) ZGLOptionsModel *subOptionsModel;


@end

@implementation ZGLSearchTableViewCell



- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.chooseButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.chooseButton];
        [self.contentView addSubview:self.titleLabel];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.backgroundColor = foreColor;
    [self.chooseButton setFrame:CGRectMake(self.contentView.frame.size.width - ScaleX * 40, ScaleX * 15, ScaleX * 30, ScaleY * 30)];
    [self.titleLabel setFrame: CGRectMake(ScaleX * 20, ScaleY * 20, ScaleX * 300, ScaleY * 20)];
    self.titleLabel.textColor = artiColor;
    [self.chooseButton setImage:[UIImage imageNamed:@"dingyue"] forState:(UIControlStateNormal)];
    [self.chooseButton setImage:[UIImage imageNamed:@"sdingyue"] forState:(UIControlStateSelected)];
    if ([self judgeIsYESORNO]) {
        _chooseButton.selected = YES;
    } else {
        _chooseButton.selected = NO;
    }
    [self.chooseButton addTarget:self action:@selector(button:) forControlEvents:(UIControlEventTouchUpInside)];
}

- (void)button:(UIButton *)button {
    if (button.selected == YES) {
        [button setSelected:NO];
        [self delegateLocalSignplist];
    } else {
        [button setSelected:YES];
        [self readLocalSignplist];
    }
    
}


- (void)setSearchWebModel:(ZGLSearchWebModel *)searchWebModel {
    if (_searchWebModel != searchWebModel) {
        _searchWebModel = searchWebModel;
    }
    _titleLabel.text = searchWebModel.name;
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    self.signArr = [user arrayForKey:@"allOptions"].mutableCopy;
    for (NSDictionary *dic in _signArr) {
        if ([dic[@"title"] isEqualToString:searchWebModel.name]) {
            _subOptionsModel = [ZGLOptionsModel modWithDic:dic];
        }
    }
}


- (BOOL)judgeIsYESORNO {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    self.signArr = [userDefaults arrayForKey:@"currentArr"].mutableCopy;
    for (NSDictionary *dic in self.signArr) {
        if ([dic[@"title"] isEqualToString:self.titleLabel.text]) {
            return YES;
        }
    }
    return NO;
}

#pragma mark - 沙盒plist写入
- (void)readLocalSignplist {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    self.signArr = [userDefaults arrayForKey:@"currentArr"].mutableCopy;
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:_subOptionsModel.title forKey:@"title"];
    [dic setObject:_subOptionsModel.large_pic forKey:@"large_pic"];
    [dic setObject:_subOptionsModel.block_color forKey:@"block_color"];
    [dic setObject:_subOptionsModel.api_url forKey:@"api_url"];
    [dic setObject:_subOptionsModel.data_type forKeyedSubscript:@"data_type"];
    [self.signArr addObject:dic];
    [userDefaults setObject:_signArr forKey:@"currentArr"];
    [userDefaults synchronize];
}

- (void)delegateLocalSignplist {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    self.signArr = [userDefaults arrayForKey:@"currentArr"].mutableCopy;
    NSMutableArray *newArr = [NSMutableArray arrayWithCapacity:0];
    for (NSDictionary *dic in _signArr) {
        if ([dic[@"title"] isEqualToString:_subOptionsModel.title]) {
            
        } else {
            [newArr addObject:dic];
        }
    }
    [userDefaults setObject:newArr forKey:@"currentArr"];
}






- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
